package com.hoshiibuy.brandcategory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.brandcategory.entity.GoodsCategory;
import com.hoshiibuy.brandcategory.entity.param.GoodsCategoryQueryParam;
import com.hoshiibuy.brandcategory.entity.param.GoodsCategoryUpdateParam;
import com.hoshiibuy.brandcategory.entity.param.GoodsCategoryAddParam;
import com.hoshiibuy.brandcategory.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品类目表 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-30
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/brandcategory/goods-category")
@Api(value = "商品类目及品牌管理", tags = {"商品类目表管理"})
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询商品类目表
    */
    @ApiOperation(value = "分页查询商品类目表")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid GoodsCategoryQueryParam param) {
        try {
            log.info("分页查询商品类目表");
            QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<GoodsCategory>();
            queryWrapper.eq(param.getCompanyid() != null, "companyid", param.getCompanyid());
            queryWrapper.eq(param.getParentId() != null, "parent_id", param.getParentId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
            queryWrapper.eq(param.getWeigth() != null, "weigth", param.getWeigth());
            queryWrapper.eq(param.getLevel() != null, "level", param.getLevel());
            queryWrapper.eq(param.getOrdered() != null, "ordered", param.getOrdered());
            queryWrapper.eq(StringUtils.isNotBlank(param.getImages()), "images", param.getImages());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRemark()), "remark", param.getRemark());
            queryWrapper.eq(param.getIsLeaf() != null, "is_leaf", param.getIsLeaf());
            queryWrapper.eq(param.getIsDel() != null, "is_del", param.getIsDel());
            IPage<GoodsCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(GoodsCategory.class));
            return Response.data(goodsCategoryService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询商品类目表异常", e);
            return Response.failed("分页查询商品类目表异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询商品类目表
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品类目表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(goodsCategoryService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品类目表")
    public Response save(@RequestBody @Valid GoodsCategoryAddParam param) {
        try {
            GoodsCategory data = BeanUtils.covertType(param, GoodsCategory.class);
            return Response.data(goodsCategoryService.save(data));
        } catch (ServiceException e) {
            log.error("新增商品类目表异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增商品类目表异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改商品类目表
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品类目表")
    public Response update(@RequestBody @Valid GoodsCategoryUpdateParam param) {
        try {
            GoodsCategory data = BeanUtils.covertType(param, GoodsCategory.class);
            return Response.data(goodsCategoryService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改商品类目表异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改商品类目表异常", e);
            return Response.failed();
        }
    }
}
